home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* copy_texure.c
- * Simple example of how to copy pixels from the frame buffer into
- * a portion of a bound texture.
- *
- * Left Mouse Button - copy pixels under cursor
- * <o> key - cycle through texture objects
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
-
- GLvoid initTexture();
-
- void printHelp( char *progname );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static unsigned int curname = 0;
- static GLuint texnames[4];
- static GLclampf priorities[4] = { 0.0, 1.0, 1.0, 1.0 };
-
- static GLuint redtex[64*64], greentex[64*64], bluetex[64*64];
- static GLuint gridtex[256][256];
-
- static GLsizei winheight;
-
- static GLboolean copy_texture_supported = GL_TRUE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- winheight = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, winheight / 4 );
- glutInitWindowSize( width / 2, winheight / 2 );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- initTexture();
-
- glutMouseFunc( mouse );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates the texture_object, copy_texture "
- "and subtexture extensions\n"
- "Left Mouse Button - copy pixels under cursor\n"
- "<o> key - cycle through texture objects\n"
- "Escape key - exit the program\n\n",
- progname );
-
- printf("Press a mouse button to copy the 32x32 square of pixels "
- "under the mouse\nto the upper right corner of the currently "
- "bound texture object.\n");
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
-
- if (!glutExtensionSupported( "GL_EXT_texture_object" )) {
- fprintf(stderr, "GL_EXT_texture_object not supported\n");
- }
- if (!glutExtensionSupported( "GL_EXT_copy_texture" )) {
- fprintf(stderr, "GL_EXT_copy_texture not supported\n");
- copy_texture_supported = GL_FALSE;
- }
- if (!glutExtensionSupported( "GL_EXT_subtexture" )) {
- fprintf(stderr,
- "GL_EXT_subtexture not advertized.. trying anyway\n");
- }
- }
-
- static void
- makeTextures(void)
- {
- int i, j;
- unsigned int *p;
-
- /* fill textures */
- for (i=0; i < 64*64; i++) {
- redtex[i] = 0xff000000;
- greentex[i] = 0x00ff0000;
- bluetex[i] = 0x0000ff00;
- }
-
- /* texture is a yellow grid */
- p = &gridtex[0][0];
- for (i=0; i<256; i++) {
- for (j=0; j<256; j++) {
- if (i%8 == 0 || j%8 == 0) {
- *p++ = 0xffff00ff;
- } else {
- *p++ = 0x808080ff;
- }
- }
- }
- }
-
- GLvoid
- initTexture( GLubyte *image, GLsizei imageWidth, GLsizei imageHeight )
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- makeTextures();
-
- /* get some unused texture names */
- glGenTexturesEXT(4, texnames);
-
- /* bind, then define, each texture */
- glBindTextureEXT(GL_TEXTURE_2D, texnames[0]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- redtex);
- glBindTextureEXT(GL_TEXTURE_2D, texnames[1]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- greentex);
- glBindTextureEXT(GL_TEXTURE_2D, texnames[2]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- bluetex);
-
- glBindTextureEXT(GL_TEXTURE_2D, texnames[3]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, gridtex);
-
- /* optional; define a working set by giving some textures a higher */
- /* priority than others */
- glPrioritizeTexturesEXT(3, texnames, priorities);
-
- /* use default texture environment, modulation */
- glEnable( GL_TEXTURE_2D );
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
- glBindTextureEXT(GL_TEXTURE_2D, texnames[curname]);
- if (copy_texture_supported) {
- glCopyTexSubImage2DEXT(GL_TEXTURE_2D, 0, 32, 32,
- x-16, winheight-y-16,
- 32, 32);
- } else {
- fprintf(stderr, "GL_EXT_copy_texture not supported\n");
- }
-
- glutPostRedisplay();
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'o':
- curname = (curname+1) % 3;
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height );
-
- winheight = height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( -3.0, 3.0, -3.0, 3.0, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
-
-
- GLvoid
- drawScene(void)
- {
- glClear( GL_COLOR_BUFFER_BIT );
-
- glColor4f( 1.0, 1.0, 1.0, 1.0 );
- glBindTextureEXT(GL_TEXTURE_2D, texnames[3]);
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2f(0,0); glVertex2f(-2,-2);
- glTexCoord2f(0,1); glVertex2f(-2, 2);
- glTexCoord2f(1,0); glVertex2f( 2,-2);
- glTexCoord2f(1,1); glVertex2f( 2, 2);
- glEnd();
-
- glBindTextureEXT(GL_TEXTURE_2D, texnames[curname]);
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2f(0,0); glVertex2f(-1,-1);
- glTexCoord2f(0,1); glVertex2f(-1, 1);
- glTexCoord2f(1,0); glVertex2f( 1,-1);
- glTexCoord2f(1,1); glVertex2f( 1, 1);
- glEnd();
-
- glFlush();
- }
-